package com.example.administrator.xxnapp.fragment.tucao;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.xxnapp.R;
import com.example.administrator.xxnapp.bean.CommentListBean;
import com.example.administrator.xxnapp.main.MainActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Went_Gone on 2018/5/7.
 */

public class RVCommentAdapter extends RecyclerView.Adapter{
    private static final String TAG = "RVCommentAdapter";
    private Context context;
    private List<CommentListBean> mDatas;
    private static final int NORMAL = 2;
    private static final int REPLY = 3;

    public RVCommentAdapter(Context context, List<CommentListBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }

    @Override
    public int getItemViewType(int position) {
//        return NORMAL;
        if (mDatas.get(position).getCommentType() == 0){
            return NORMAL;
        }else {
            return REPLY;
        }
//        return mDatas.get(position).getCommentType();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        switch (viewType){
            case NORMAL:
                holder = new RVCommentViewHolder(LayoutInflater.from(context).inflate(R.layout.item_tucao_comment_normal_layout,parent,false));
                break;
            case REPLY:
                holder = new RVReplyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_tucao_comment_reply_layout,parent,false));
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof RVCommentViewHolder){
            ((RVCommentViewHolder) holder).bindView(position);
        }else if (holder instanceof RVReplyViewHolder){
            ((RVReplyViewHolder) holder).bindView(position);
        }
    }

    @Override
    public int getItemCount() {
        return mDatas == null?0:mDatas.size();
    }

    private class RVCommentViewHolder extends RecyclerView.ViewHolder{
        private TextView mTV;
        private View mViewRoot;

        public RVCommentViewHolder(View itemView) {
            super(itemView);
            mTV = itemView.findViewById(R.id.item_tucao_comment_normal_layout_TV);
            mViewRoot = itemView.findViewById(R.id.item_tucao_comment_normal_layout_Root);
        }

        private void bindView(final int position){
            CommentListBean bean = mDatas.get(position);
            final List<String> strings = new ArrayList<>();
            strings.add(bean.getCommentUserName());
            strings.add(":");
            strings.add(bean.getContent());
            String all = strings.get(0)+strings.get(1)+strings.get(2);
            SpannableString spannableString0 = new SpannableString(all);
            spannableString0.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.base_color)),all.indexOf(strings.get(0)),all.indexOf(":"),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            MyClickableSpan clickableSpan0 = new MyClickableSpan(position,0);
            spannableString0.setSpan(clickableSpan0, 0, strings.get(0).length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            mTV.setText(spannableString0);
            mTV.setMovementMethod(CustomLinkMovementMethod.getInstance());

            mViewRoot.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onCommentItemClickListener != null){
                        onCommentItemClickListener.onItemClick(position,0);
                    }
                }
            });
        }
    }

    class MyClickableSpan extends ClickableSpan {
        private int position;
        private int idType;

        public MyClickableSpan(int position, int idType) {
            this.position = position;
            this.idType = idType;
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setUnderlineText(false);
        }

        @Override
        public void onClick(View widget) {
            if (idType == 0){
                //前边
                if (onCommentItemClickListener !=null){
                    onCommentItemClickListener.onItemUserClick(position,mDatas.get(position).getCommentUserId());
                }
            }else {
                //后边
                if (onCommentItemClickListener !=null){
                    onCommentItemClickListener.onItemUserClick(position,mDatas.get(position).getAtUserId());
                }
            }
        }
    }


    private class RVReplyViewHolder extends RecyclerView.ViewHolder{
        private TextView mTV;
        private View mViewRoot;

        public RVReplyViewHolder(View itemView) {
            super(itemView);
            mTV = itemView.findViewById(R.id.item_tucao_comment_reply_layout_TV);
            mViewRoot = itemView.findViewById(R.id.item_tucao_comment_reply_layout_Root);
        }

        private void bindView(final int position){
            CommentListBean bean = mDatas.get(position);
            final List<String> strings = new ArrayList<>();
            strings.add(bean.getCommentUserName());
            strings.add("回复");
            strings.add(bean.getAtUserName());
            strings.add(":");
            strings.add(bean.getContent());

            String all = strings.get(0)+strings.get(1)+strings.get(2)+strings.get(3)+strings.get(4);

            SpannableString spannableString0 = new SpannableString(all);

            spannableString0.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.base_color)),0,all.indexOf("回复"),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannableString0.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.base_color)),strings.get(0).length()+strings.get(1).length(),all.indexOf(":"),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

            MyClickableSpan clickableSpan0 = new MyClickableSpan(position,0);
            spannableString0.setSpan(clickableSpan0, 0, strings.get(0).length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            MyClickableSpan clickableSpan1 = new MyClickableSpan(position,1);
            spannableString0.setSpan(clickableSpan1, all.indexOf(strings.get(2)), all.indexOf(":"), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            mTV.setText(spannableString0);
            mTV.setMovementMethod(CustomLinkMovementMethod.getInstance());

            mViewRoot.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onCommentItemClickListener != null){
                        onCommentItemClickListener.onItemClick(position,1);
                    }
                }
            });
        }
    }
    private OnCommentItemClickListener onCommentItemClickListener;


    public void setOnCommentItemClickListener(OnCommentItemClickListener onCommentItemClickListener) {
        this.onCommentItemClickListener = onCommentItemClickListener;
    }

    public interface OnCommentItemClickListener{
        void onItemUserClick(int position,int userId);
        void onItemClick(int position,int commentType);
    }

    public static class CustomLinkMovementMethod extends LinkMovementMethod{

        @Override
        public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
            boolean b = super.onTouchEvent(widget,buffer,event);
            //解决点击事件冲突问题
            if(!b && event.getAction() == MotionEvent.ACTION_UP){
                ViewParent parent = widget.getParent();//处理widget的父控件点击事件
                if (parent instanceof ViewGroup) {
                    return ((ViewGroup) parent).performClick();
                }
            }
            return b;
        }

        public static CustomLinkMovementMethod getInstance() {
            if (sInstance == null)
                sInstance = new CustomLinkMovementMethod();

            return sInstance;
        }


        private static CustomLinkMovementMethod sInstance;

    }
}
